<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
    <style>

    </style>
</head>

<body>
    <div id="app">
        <child1></child1>
        <child2></child2>
    </div>
</body>

<template id="tmpl1">
    <div>
        <h1>{{txt}}</h1>
    </div>
</template>

<template id="tmpl2">
    <div>
        <button @click="hdClick">change</button>
    </div>
</template>

<script>
    // 事件总线
    let bus = new Vue()

    let child1 = {
        template: '#tmpl1',
        data() {
            return {
                txt: '111'
            }
        },
        created() {
            //组件创建开始监听
            bus.$on('change', val => {
                this.txt = val
            })
        },
    }
    let child2 = {
        template: '#tmpl2',
        methods: {
            //通过点击事件  触发总线change先关监听并传递数据。注意数据是可选填项
            hdClick() {
                bus.$emit('change', '22222')
            }
        },
    }

    var vm = new Vue({
        el: '#app',
        data: {},
        components: {
            child1,
            child2
        }
    })
</script>

</html>